//
//  RegistrationPointViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/25.
//

#import "RegistrationPointViewController.h"
#import "OneTabButView.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookUploadImageCell.h"
#import "LongUIImagePickerController.h"
@interface RegistrationPointViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation RegistrationPointViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navgationheadv.titlelabel.text = @"报名点信息";
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"保存" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];

    titlearr = @[@"报名点名称",@"报名地址",@"报名地址照片"];
    promptarr = @[@"请输入报名点名称",@"请选择报名地址",@""];
    celltypearr = @[@"1",@"1",@"8"];

    for (NSInteger i=0; i<titlearr.count; i++) {
        CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
        model.title = titlearr[i];
        model.prompt = promptarr[i];
        model.cellType = [celltypearr[i] integerValue];
        model.showArrow = NO;
        model.showPhotoImage = NO;
        if (i==1)
        {
            model.showArrow = YES;
        }
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-110);
    
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细信息";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
    

}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataArray.count-1) {
        return (APPwidth-32)*200/328+60;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CoachsBookInputModel *model = self.dataArray[indexPath.row];
    if (model.cellType == 1) {
        CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
        if (!cell) {
            cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.contentField.tag = indexPath.row;
        cell.contentField.delegate = self;
        return cell;
    }
    
    CoachsBookUploadImageCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookUploadImageCell"];
    if (!cell) {
        cell=[[CoachsBookUploadImageCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookUploadImageCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellFillWithModel:model];
    [cell.cameraBut1 addTarget:self action:@selector(cameraButsCheng:) forControlEvents:UIControlEventTouchUpInside];
    cell.cameraBut1.tag = indexPath.row;
    UITapGestureRecognizer *photoImage1tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photoImagetapCheng:)];
    cell.photoImage1.tag = indexPath.row;
    [cell.photoImage1 addGestureRecognizer:photoImage1tapGestureRecognizer];
    return cell;
    
}

#pragma mark -陪练服务开关
-(void)SwitchButCheng:(UISwitch *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    if (but.on) {
        model.content = @"1";
        [LongHelper poppingAlertWithReminderTitle:@"" message:@"陪练服务已关闭，学员将不能预约您的陪练服务" AndActionTitle:@"我知道了" ActionHandler:^(UIAlertAction *action) {
        } ViewController:self];
    }
    else{
        model.content = @"0";
        [LongHelper poppingAlertWithReminderTitle:@"" message:@"陪练服务已打开，学员将会通过您发布陪练服务预约" AndActionTitle:@"我知道了" ActionHandler:^(UIAlertAction *action) {
        } ViewController:self];
    }
    [self getData];
}

#pragma mark -单选按钮
-(void)lefttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"1";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:but.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

-(void)righttabbutCheng:(UIButton *)but
{
    CoachsBookInputModel *model = self.dataArray[but.tag];
    model.content = @"2";
    [self.view endEditing:YES];
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:but.tag inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        NSMutableArray *PickerViewModelArr = [[NSMutableArray alloc]init];
        [PickerViewModelArr removeAllObjects];
        if (textField.tag == 1) {

        }
    }
    
}


#pragma mark - 拍照选择图片
-(void)cameraButsCheng:(UIButton *)but
{
    [LongHelper poppingBottomAlertWithReminderTitle:nil message:nil AndOneActionTitle:@"拍照" OneActionHandler:^(UIAlertAction *action)
     {
        [self takePhotoWithTag:but.tag];
     } AndTwoActionTitle:@"从手机相册选择" TwoActionHandler:^(UIAlertAction *action) {
         [self selectPhotoWithTag:but.tag];
     } AndCancelActionTitle:@"取消" NoActionHandler:^(UIAlertAction *action) {
         
     } ViewController:self];
}
#pragma mark - 拍照选择图片
-(void)photoImagetapCheng:(UITapGestureRecognizer *)tap
{
    UIView *view = tap.view;
    NSLog(@"点击了第%ld个view",view.tag);
    [LongHelper poppingBottomAlertWithReminderTitle:nil message:nil AndOneActionTitle:@"拍照" OneActionHandler:^(UIAlertAction *action)
     {
        [self takePhotoWithTag:view.tag];
     } AndTwoActionTitle:@"从手机相册选择" TwoActionHandler:^(UIAlertAction *action) {
         [self selectPhotoWithTag:view.tag];
     } AndCancelActionTitle:@"取消" NoActionHandler:^(UIAlertAction *action) {
         
     } ViewController:self];
}


#pragma mark - 相机拍照
- (void)takePhotoWithTag:(NSInteger)tag
{
    LongUIImagePickerController *imagePickerController = [[LongUIImagePickerController alloc]init];
    imagePickerController.tag = tag;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;    //设置来源为相机
    imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;  //设置后置摄像头
    imagePickerController.delegate = self;
//    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
}


#pragma mark - 相册选择相片
- (void)selectPhotoWithTag:(NSInteger)tag
{
    LongUIImagePickerController *imagePickerController = [[LongUIImagePickerController alloc]init];
    imagePickerController.tag = tag;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;    //设置来源为相册
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;   //是否显示裁剪框编辑
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    LongUIImagePickerController *imagePickerController = (LongUIImagePickerController*)picker;
    NSLog(@"点击了第%ld个imagePickerController",imagePickerController.tag);
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    CoachsBookInputModel *model = self.dataArray[imagePickerController.tag];
    model.photoImage = image;
    model.showPhotoImage = YES;
    model.photoImageurl = @"";
    [self.tableView reloadData];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}

#pragma mark - CYWKLPickerViewDelegate
-(void)choosPickerView:(UIPickerView *)pickerView WithRowStr:(NSString *)rowStr AndRowNum:(NSInteger)row
{
    CoachsBookInputModel *model = self.dataArray[1];
    model.content = rowStr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = rowStr;
    [self.view endEditing:YES];
    
}

-(void)closePickerView:(UIPickerView *)pickerView
{
    [self.view endEditing:YES];
}


#pragma mark -OneTabButViewDelegate(保存)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}

@end
